package com.ms.oschina.task;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import okhttp3.Cookie;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.Response;

import com.ms.oschina.api.remote.OkHttpClientManager;
import com.ms.oschina.api.remote.OkHttpClientManager.OnGetNetResultListener;
import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.BaseAsyncTask;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.HttpResponse;
import com.ms.oschina.bean.LoginUserBean;
import com.ms.oschina.bean.Result;
import com.ms.oschina.bean.ResultBean;
import com.ms.oschina.util.XmlUtils;

public class UploadTask extends BaseAsyncTask<Result> {
	private int requestCode;
	private String url;
	private OnGetNetResultListener<Result> listener;
	private String uploadPath;
	private String message;
	private String uid;

	public UploadTask(String url, int requestCode, String uid, String message,
			String uploadPath, OnGetNetResultListener<Result> listener) {
		this.url = url;
		this.requestCode = requestCode;
		this.uid = uid;
		this.message = message;
		this.uploadPath = uploadPath;
		this.listener = listener;
	}

	@Override
	protected Result exeCute() throws Exception {
		// 请求登录
		String responseStr = requestUpload();
		ResultBean resultBean = XmlUtils.toBean(ResultBean.class, responseStr.getBytes());
		Result result = resultBean.getResult();
		String errorMessage = result.getErrorMessage();
		setObject(errorMessage);
		int errorCode = result.getErrorCode();
		setErrorCode(errorCode);// 封装错误码
		return result;
	}

	private String requestUpload() throws IOException {
		String boundary = UUID.randomUUID().toString();
		//MultipartBody.Builder(boundary) 等于：Content-Type: multipart/form-data; boundary=00b791cf-0be2-47f3-823a-0fb6e1421652
		MultipartBody body = new MultipartBody.Builder(boundary)
				.setType(MultipartBody.FORM)
				.addPart(
						Headers.of("Content-Disposition",
								"form-data; name=\"uid\""),
						RequestBody.create(OkHttpClientManager.textPlain, uid))
						//参数一（多部件请求头）：  OkHttpClientManager.textPlain
						//Content-Disposition: form-data; name="uid"
						//Content-Type: text/plain; charset=utf-8
						//Content-Length: 7
						
						//2823808
				.addPart(
						Headers.of("Content-Disposition",
								"form-data; name=\"msg\""),
						RequestBody.create(OkHttpClientManager.textPlain,
								message))
				.addPart(
						Headers.of("Content-Disposition",
								"form-data; name=\"img\"; filename=\"tempfile.jpg\""),
						RequestBody.create(
								MediaType.parse("application/octet-stream"),
								new File(uploadPath))).build();

		String responseStr = OkHttpClientManager.getInstance()
				.postSyncAsString(url, body);
		return responseStr;
	}

	@Override
	protected void onPostExe(HttpResponse<Result> result) {
		listener.onGetNetResult(requestCode, result);
	}
}